﻿@{
    Layout = "";
}

<div class="signin" ng-controller="LoginController">
    <link href="/Content/css/bootstrap.min.css" rel="stylesheet">
    <link href="/Content/css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="/Content/css/animate.min.css" rel="stylesheet">
    <link href="/Content/css/style.min.css" rel="stylesheet">
    <link href="/Content/css/login.min.css" rel="stylesheet">
    <!--[if lt IE 8]>
        <meta http-equiv="refresh" content="0;ie.html"/>
    <![endif]-->
    <script>
        if (window.top !== window.self) {
            window.top.location = window.location
        };
    </script>
    <div class="signinpanel">
        <div class="row">
            <div class="col-sm-7">
                <div class="signin-info">
                    <div class="logopanel m-b">
                        <h1>[ Welcome ]</h1>
                    </div>
                    <div class="m-b"></div>
                    <h4>欢迎使用 <strong>@Infrastructure.Pub.PubConst.SiteName</strong></h4>
                    <ul class="m-b">
                        @*<li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> 优势一</li>
                            <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> 优势二</li>
                            <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> 优势三</li>
                            <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> 优势四</li>

                            <li><i class="fa fa-arrow-circle-o-right m-r-xs"></i> 优势五</li>*@
                    </ul>
                    @*<strong>还没有账号？ <a href="#">立即注册&raquo;</a></strong>*@
                </div>
            </div>
            <div class="col-sm-5">
                <form id="form1">
                    <h4 class="no-margins">登录：</h4>
                    <p class="m-t-md">登录到 @Infrastructure.Pub.PubConst.SiteName</p>
                    <input type="text" id="userName" name="userName" class="form-control uname" placeholder="用户名"  maxlength="20" />
                    <input type="password" id="passWord" name="passWord" class="form-control pword m-b" placeholder="密码"  maxlength="20" />

                        <input type="text" id="validateCode" name="validateCode" class="form-control glyphicon glyphicon-ok" placeholder="验证码" maxlength="20" />
                        <img id="valiCode" style="cursor: pointer;width:180px;height:40px" class="col-md-2 control-label" src="~/Account/GetValidateCode" alt="验证码" ng-click="getValidateCode()" />


                    @*<a href="">忘记密码了？</a>*@
                    <button type="button" id="btn_login" class="btn btn-success btn-block" ng-click="LoginSubmit()">登录</button>
                </form>
            </div>
        </div>
        <div class="signup-footer">
            <div class="pull-left">
                &copy; @(DateTime.Now.Year) All Rights Reserved. WSD
            </div>
        </div>
    </div>
</div>
@*<script src="~/Content/plugins/layer-master/build/layer.js"></script>*@
<script src="~/Content/js/plugins/validate/jquery.validate.min.js"></script>
<script src="~/Content/js/plugins/validate/messages_zh.min.js"></script>
<script>
    App.controller('LoginController', ["$scope", "$state", "$http", "$stateParams", "Notify", "$filter", "$rootScope", "$cookieStore", "$window",
    function ($scope, $state, $http, $stateParams, Notify, $filter, $rootScope, $cookieStore, $window) {

        $("#form1").validate({
            onfocusout: function (element) {
                $(element).valid();
            },
            rules: {
                userName: {
                    required: true,
                    maxlength: 50
                },
                passWord: {
                    required: true,
                    maxlength: 50
                },
                validateCode: {
                    required: true,
                    maxlength: 10
                }
            },
            messages: {
                userName: {
                    required: "请输入用户名"
                },
                passWord: {
                    required: "请输入密码"
                },
                validateCode: {
                    required: "请输入验证码"
                }
            }
        });
        $("body").keydown(function () {
            if (event.keyCode == "13") {
                $('#btn_login').click();
            }
        });


        $scope.LoginSubmit = function () {
            if (!$("#form1").valid()) {
                return false;
            }

            var username = $("#userName").val();
            var password = $("#passWord").val();
            var validateCode = $("#validateCode").val();
            layer.load();
            $.ajax({
                type: "POST",
                url: "/Account/LoginSubmit/",
                dataType: "json",
                data: { userName: username, passWord: password, validateCode: validateCode },
                success: function (data) {
                    layer.closeAll('loading');
                    if (data.isSuccess) {
                        layer.msg(data.msg, { time: 1000 });
                        //window.location.href = "/#/app/dashboard";
                        $http.post('/Account/GetUserInfo', { userName: username }).then(function (response) {

                            //$rootScope.user = {
                            //    Id: response.data.Id,
                            //    UserName: response.data.UserName,
                            //    RealName: response.data.RealName
                            //};

                            var date = new Date();
                            var welcomestr = "";
                            if (date.getHours() > 12) {
                                welcomestr = " 下午好，欢迎登陆";
                            }
                            else {
                                welcomestr = " 上午好，欢迎登陆";
                            }

                            if (response.data.isTeacher == 1) {
                                identity = '老师';
                            }
                            else {
                                identity = '学生';
                            }

                            $rootScope.user = {
                                //name: response.data.Phone,
                                //   job: response.data.Roles[0],
                                //   picture: response.data.Company,
                                Id: response.data.Id,
                                UserName: response.data.UserName,
                                RealName: response.data.RealName + welcomestr,
                                GroupName: response.data.GroupName,
                                Picture: response.data.Picture,
                                Identity: identity,
                                DepartmentName: response.data.DepartmentName
                            };

                            $cookieStore.put('currentUser', $rootScope.user);
                            //$rootScope.IsDisplayAd = true;
                            //$cookieStore
                            if ($stateParams.redirect == undefined || $stateParams.redirect === "") {
                                $state.go("app.homepage");
                            } else {
                                $window.location.href = $stateParams.redirect;
                                window.location.reload();
                            }
                        });

                    } else {
                        layer.msg(data.msg);
                    }
                },
                error: function () {
                    layer.closeAll('loading');
                    layer.msg("登录失败");
                }

            });


        };

        $scope.getValidateCode = function () {
            var img = document.getElementById('valiCode');
            
            valiCode.src = "../Account/GetValidateCode?time=" + (new Date()).getTime();
        };

    }]);
</script>
